function add0(m){return m<10?'0'+m:m }
function format(shijianchuo)
{
//shijianchuo是整数，否则要parseInt转换
var time = new Date(shijianchuo);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
}

var rootPath = getRootPath();
$(document).ready(function(){
	
	var testdriveAppointmentId = getQueryParamValue("testdriveAppointmentId");
	var userId = getQueryParamValue("userId");
	var potentialUserId = getQueryParamValue("potentialUserId");
	$("#testdriveAppointmentId").val(testdriveAppointmentId);
	$("#userId").val(userId);
	
	
	$.ajax({
		method : 'get',
		url : rootPath+'/wechat/api/testdrivingAppointment/'+testdriveAppointmentId,
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("连接服务器错误!");
		},
		success : function(resultData){
			
			var wholeHtml = '';
			
			$.each(resultData,function(testdrivingIndex,testdrivingObj){
				
				var testdrivingHtml = '';
				
				if(testdrivingObj.status=='待确认'||testdrivingObj.status=='待试驾'){
					
					testdrivingHtml = testdrivingHtml + '<div class="panel panel-default">'
													  +   '<ul class="list-group">'
													  +     '<li class="list-group-item">试驾车型：'+testdrivingObj.packageCodeName+'</li>'
													  +		'<li class="list-group-item">'
													  +		  '<span>联&nbsp;&nbsp;系&nbsp;&nbsp;人：'+testdrivingObj.personalName+'</span><br><br>'
													  +		  '<span>手机号码：'+testdrivingObj.phone+'</span><br><br>'
													  +		  '<span>预约时间：'+format(testdrivingObj.createDate)+'</span><br><br>'
													  +		  '<span>试驾时间：'+format(testdrivingObj.startDate)+'</span><br><br>'
													  +		 '</li>'
													  +    '</ul>'
													  + '</div>'
													  + '<div class="panel panel-default">'
													  +   '<ul class="list-group">'
													  +     '<li class="list-group-item">'
													  +		  '预约状态：<span style="color:#F15C5C">'+testdrivingObj.status+'</span><br><br>'
													  +		  '<small style="color:#B7B7B7">我们将以最快速度为您安排，请稍作等候。</small><br><br>'
													  +		'</li>'
													  +    '</ul>'
													  + '</div>'
													  + '<div class="panel panel-default">'
													  +   '<input style="background-color:#D2D2D2;color:white" type="button" data-toggle="modal" data-target="#myModal"  value="取消预约" class="btn btn-default btn-lg btn-block" />'
													  + '</div>'
				}
				else if(testdrivingObj.status=='已试驾'){
					
					testdrivingHtml = testdrivingHtml + '<div class="panel panel-default">'
					  +   '<ul class="list-group">'
					  +     '<li class="list-group-item">试驾车型：'+testdrivingObj.packageCodeName+'</li>'
					  +		'<li class="list-group-item">'
					  +		  '<span>联&nbsp;&nbsp;系&nbsp;&nbsp;人：'+testdrivingObj.personalName+'</span><br><br>'
					  +		  '<span>手机号码：'+testdrivingObj.phone+'</span><br><br>'
					  +		  '<span>预约时间：'+format(testdrivingObj.createDate)+'</span><br><br>'
					  +		  '<span>试驾时间：'+format(testdrivingObj.startDate)+'</span><br><br>'
					  +		 '</li>'
					  +    '</ul>'
					  + '</div>'
					  + '<div class="panel panel-default">'
					  +   '<ul class="list-group">'
					  +     '<li class="list-group-item">'
					  +		  '预约状态：<span style="color:#76CBFF">'+testdrivingObj.status+'</span><br><br>'
					  +		  '<small style="color:#B7B7B7">您已完成试驾，感谢您的光临。</small><br><br>'
					  +		'</li>'
					  +    '</ul>'
					  + '</div>'
				}
				else{ 
					
					testdrivingHtml = testdrivingHtml + '<div class="panel panel-default">'
					  +   '<ul class="list-group">'
					  +     '<li class="list-group-item">试驾车型：'+testdrivingObj.packageCodeName+'</li>'
					  +		'<li class="list-group-item">'
					  +		  '<span>联&nbsp;&nbsp;系&nbsp;&nbsp;人：'+testdrivingObj.personalName+'</span><br><br>'
					  +		  '<span>手机号码：'+testdrivingObj.phone+'</span><br><br>'
					  +		  '<span>预约时间：'+format(testdrivingObj.createDate)+'</span><br><br>'
					  +		  '<span>试驾时间：'+format(testdrivingObj.startDate)+'</span><br><br>'
					  +		 '</li>'
					  +    '</ul>'
					  + '</div>'
					  + '<div class="panel panel-default">'
					  +   '<ul class="list-group">'
					  +     '<li class="list-group-item">'
					  +		  '预约状态：<span style="color:#B7B7B7">'+testdrivingObj.status+'</span><br><br>'
					  +		  '<small style="color:#B7B7B7">您的试驾预约'+ testdrivingObj.status +'。</small><br><br>'
					  +		'</li>'
					  +    '</ul>'
					  + '</div>'
				}				  
				
				wholeHtml += testdrivingHtml;
			});
			
			$("#information").append(wholeHtml);
		}
	});	
	
	$('#modalSure').click(function(){
		var data={
		appointmentStatus:"",
		updateDate:"",
		userId:"",
		testdriveAppointmentId:"",
    }; 
    data.appointmentStatus = $.trim($("#appointmentStatus").val());
    var myDate = new Date();
    var result=myDate.getFullYear()+'-'+(myDate.getMonth()+1)+'-'+myDate.getDate() ;
    data.updateDate = myDate.getFullYear()+'-'+(myDate.getMonth()+1)+'-'+myDate.getDate()+' '+myDate.getHours()+':'+myDate.getMinutes() ;
    data.userId = $.trim($("#userId").val());
    data.testdriveAppointmentId = $.trim($("#testdriveAppointmentId").val());
     
	$.ajax({
		type : 'POST',
		url : rootPath+'/wechat/api/testdrivingAppointment/updateStatus',
		contentType:"application/json",
		data: JSON.stringify(data),
		error:function(XMLHttpRequest, ajaxOptions, thrownError){
			alert("失败");
		},
		success : function(data){
			$('#testdrivingModal').modal({
				keyboard: false,
				backdrop:false
			});
		}
	});
  });
})
function re(){
	window.location = rootPath + "/views/hp.html";
}
